#### DATA BASES ##

whole_pop <- read.csv("C:/Users/Christophe/Desktop/WHOLE.csv", sep=",", dec = ".")
tk_mor <- read.csv("C:/Users/Christophe/Desktop/TK MOR.csv", sep=",", dec = ".")
eu_15 <- read.csv("C:/Users/Christophe/Desktop/EU 15.csv", sep=",", dec = ".")
post_enlarg <- read.csv("C:/Users/Christophe/Desktop/EU POST 2004.csv", sep=",", dec = ".")
NON_EU_NON_TK_MOR <- read.csv("C:/Users/Christophe/Desktop/NON EU NO TK MOR.csv", sep=",", dec = ".")
HOMEOWNERSHIP <- read.csv("C:/Users/Christophe/Desktop/HOMEOWNERSHIP.csv", sep=",", dec = ".")
NO_HOMEOWNERSHIP <- read.csv("C:/Users/Christophe/Desktop/NO HOMEOWNERSHIP.csv", sep=",", dec = ".")
EU <- read.csv("C:/Users/Christophe/Desktop/EU.csv", sep=",", dec = ".")
NON_EU <- read.csv("C:/Users/Christophe/Desktop/NON EU.csv", sep=",", dec = ".")


#### WHOLE POP ###

library(ggplot2)
plot_whole <- ggplot(data=whole_pop, aes(x=Income, y=Poverty_margins, ymin=Low_ci, ymax=High_ci, fill=Naturalisation)) + 
  scale_fill_manual(values = c("mediumturquoise", "indianred2")) +
  geom_line() + 
  geom_ribbon(alpha=0.5) + 
  ggtitle("M 1: Whole population") +
  xlab("Income decile") + 
  ylab("Predicted percent of high-income ind.") +
  theme_minimal() +
  theme(plot.title = element_text(face="plain", size = 24, hjust = 0.5),
        axis.text=element_text(size=20),
        axis.title = element_text(size = 20),
        legend.position = "none")

plot_whole
plot_figure_1 <- plot_whole + expand_limits(y=c(51.8, 54.8))

ggsave("figure_1_paper_3.png", plot = plot_whole, width = 13, height = 11, path = "C:/", dpi = 300, limitsize = FALSE)

#### EU/NON-EU-HOMEOWNER-NOT HOMEOWNER ####

### HOMEOWNER-NOT HOMEOWNER

plot_home_owner <- ggplot(data=HOMEOWNERSHIP, aes(x=Income, y=Poverty_margins, ymin=Low_ci, ymax=High_ci, fill=Naturalisation)) + 
  scale_fill_manual(values = c("mediumturquoise", "indianred2")) +
  geom_line() + 
  geom_ribbon(alpha=0.5) + 
  ggtitle("M 2: Became a homeown.") +
  xlab("Income decile") + 
  ylab("Predicted percent of high-income ind.") +
  theme_minimal() +
  theme(plot.title = element_text(face="plain", size = 24, hjust = 0.5),
        axis.text=element_text(size=20),
        axis.title = element_text(size = 20),
        legend.position = "none") 

plot_home_owner

plot_renting <- ggplot(data=NO_HOMEOWNERSHIP, aes(x=Income, y=Poverty_margins, ymin=Low_ci, ymax=High_ci, fill=Naturalisation)) + 
  scale_fill_manual(values = c("mediumturquoise", "indianred2")) +
  geom_line() + 
  geom_ribbon(alpha=0.5) + 
  ggtitle("M 3: Never became a homeown.") +
  xlab("Income decile") + 
  ylab("Predicted percent of high-income ind.") +
  theme_minimal() +
  theme(plot.title = element_text(face="plain", size = 24, hjust = 0.5),
        axis.text=element_text(size=20),
        axis.title = element_text(size = 20),
        legend.position = "none") 

plot_renting
plot_renting <- plot_renting + expand_limits(y=c(46.5, 49.5))

library(ggpubr)
library(gridExtra)



figure_housing_type <- ggarrange(plot_home_owner, plot_renting,
                                 nrow = 2)
figure_housing_type


####### EU/NON EU

plot_EU <- ggplot(data=EU, aes(x=Income, y=Poverty_margins, ymin=Low_ci, ymax=High_ci, fill=Naturalisation)) + 
  scale_fill_manual(values = c("mediumturquoise", "indianred2")) +
  geom_line() + 
  geom_ribbon(alpha=0.5) + 
  ggtitle("M 4: EU Migrants") +
  xlab("Income decile") + 
  ylab("Predicted percent of high-income ind.") +
  theme_minimal() +
  theme(plot.title = element_text(face="plain", size = 24, hjust = 0.5),
        axis.text=element_text(size=20),
        axis.title = element_text(size = 20),
        legend.background = element_rect(colour="grey80"),
        legend.key.size = unit(22, "pt"),
        legend.text = element_text(size=20),
        legend.title = element_blank()) 

plot_EU


plot_NON_EU <- ggplot(data=NON_EU, aes(x=Income, y=Poverty_margins, ymin=Low_ci, ymax=High_ci, fill=Naturalisation)) + 
  scale_fill_manual(values = c("mediumturquoise", "indianred2")) +
  geom_line() + 
  geom_ribbon(alpha=0.5) + 
  ggtitle("M 5: Non EU Migrants") +
  xlab("Income decile") + 
  ylab("Predicted percent of high-income ind.") +
  theme_minimal() +
  theme(plot.title = element_text(face="plain", size = 24, hjust = 0.5),
        axis.text=element_text(size=20),
        axis.title = element_text(size = 20),
        legend.background = element_rect(colour="grey80"),
        legend.key.size = unit(22, "pt"),
        legend.text = element_text(size=20),
        legend.title = element_blank()) 

plot_NON_EU
plot_NON_EU <- plot_NON_EU + expand_limits(y=c(49.4, 53.1))

figure_eu_non_eu <- ggarrange(plot_EU, plot_NON_EU,
                              nrow = 2,
                              common.legend = T,
                              legend = "right")
figure_eu_non_eu


plot_figure_2 <- grid.arrange(figure_housing_type, figure_eu_non_eu, ncol = 2, nrow = 1)
plot_figure_2

ggsave("figure_2_paper_3.png", plot = plot_figure_2, width = 17, height = 12, path = "C:", dpi = 300, limitsize = FALSE)


###################################################### SUB GROUPS ############################################################


plot_eu_15 <- ggplot(data=eu_15, aes(x=Income, y=Poverty_margins, ymin=Low_ci, ymax=High_ci, fill=Naturalisation)) + 
  scale_fill_manual(values = c("mediumturquoise", "indianred2")) +
  geom_line() + 
  geom_ribbon(alpha=0.5) + 
  ggtitle("M 6: EU 15 & EFTA") +
  xlab("Income decile") + 
  ylab("Predicted percent of high-income ind.") +
  guide_legend(reverse = false) +
  theme_minimal() +
  theme(plot.title = element_text(face="plain", size = 24, hjust = 0.5),
        axis.text=element_text(size=20),
        axis.title = element_text(size = 20),
        legend.background = element_rect(colour="grey80"),
        legend.key.size = unit(22, "pt"),
        legend.text = element_text(size=20),
        legend.title = element_blank(),
        legend.position = "none") 

plot_eu_15
plot_eu_15 <- plot_eu_15 + expand_limits(y=c(57.75, 65.25))


plot_post_enlarg <- ggplot(data=post_enlarg, aes(x=Income, y=Poverty_margins, ymin=Low_ci, ymax=High_ci, fill=Naturalisation)) + 
  scale_fill_manual(values = c("mediumturquoise", "indianred2")) +
  geom_line() + 
  geom_ribbon(alpha=0.5) + 
  ggtitle("M 7: EU post 2004") +
  xlab("Income decile") + 
  ylab("Predicted percent of high-income ind.") +
  theme_minimal() +
  theme(plot.title = element_text(face="plain", size = 24, hjust = 0.5),
        axis.text=element_text(size=20),
        axis.title = element_text(size = 20),
        legend.background = element_rect(colour="grey80"),
        legend.key.size = unit(22, "pt"),
        legend.text = element_text(size=20),
        legend.title = element_blank(),
        legend.position = "none") 

plot_post_enlarg

plot_post_enlarg <- plot_post_enlarg + expand_limits(y=c(53.4, 61.4))

figure_eu_sub <- ggarrange(plot_eu_15, plot_post_enlarg,
                           nrow = 2)
figure_eu_sub


plot_tk_mor <- ggplot(data=tk_mor, aes(x=Income, y=Poverty_margins, ymin=Low_ci, ymax=High_ci, fill=Naturalisation)) + 
  scale_fill_manual(values = c("mediumturquoise", "indianred2")) +
  geom_line() + 
  geom_ribbon(alpha=0.5) + 
  ggtitle("M 8: Turks and Moroccans") +
  xlab("Income decile") + 
  ylab("Predicted percent of high-income ind.") +
  theme_minimal() +
  theme(plot.title = element_text(face="plain", size = 24, hjust = 0.5),
        axis.text=element_text(size=20),
        axis.title = element_text(size = 20),
        legend.background = element_rect(colour="grey80"),
        legend.key.size = unit(22, "pt"),
        legend.text = element_text(size=20),
        legend.title = element_blank()) 

plot_tk_mor


plot_no_tk_mor <- ggplot(data=NON_EU_NON_TK_MOR, aes(x=Income, y=Poverty_margins, ymin=Low_ci, ymax=High_ci, fill=Naturalisation)) + 
  scale_fill_manual(values = c("mediumturquoise", "indianred2")) +
  geom_line() + 
  geom_ribbon(alpha=0.5) + 
  ggtitle("M 9: Non EU (TK and Mor excl.)") +
  xlab("Income decile") + 
  ylab("Predicted percent of high-income ind.") +
  theme_minimal() +
  theme(plot.title = element_text(face="plain", size = 24, hjust = 0.5),
        axis.text=element_text(size=20),
        axis.title = element_text(size = 20),
        legend.background = element_rect(colour="grey80"),
        legend.key.size = unit(22, "pt"),
        legend.text = element_text(size=20),
        legend.title = element_blank()) 

plot_no_tk_mor


figure_non_eu_sub <- ggarrange(plot_tk_mor, plot_no_tk_mor,
                               common.legend = T,
                               nrow = 2,
                               legend = "right")
figure_non_eu_sub


plot_figure_3 <- grid.arrange(figure_eu_sub, figure_non_eu_sub, ncol = 2, nrow = 1)
plot_figure_3

ggsave("figure_3_paper_3.png", plot = plot_figure_3, width = 17, height = 12, path = "C:", dpi = 300, limitsize = FALSE)

